var dg;
var upCount;//上翻次数
var downCount;//下翻次数
$(function() {
    dg = $('#log_list');
    var total = 0;
    var pageSize = 20;
    var pageNum = 1;
    dg.datagrid({
        title:'应用日志列表',
        // url:contextPath+'/console/log/listByPageForEasyui?pageNo=1&pageSize='+pageSize,
        loadMsg: '加载中...',
        width: '100%',
        height: $("#log_list_div").height(),
        fitColumns:true,
        pagination:true,
        pageSize:pageSize,
        pageList:[5,10,20,40,80],
        singleSelect:true,
        selectOnCheck:false,
        checkOnSelect:false,
        rownumbers:true,
        emptyMsg:'暂无数据',
        //toolbar: [],
        columns:[[
            {field: 'appName', title: '应用( 节点 )名称',width: 220, halign:'center',align:'center',
            formatter:function(value,row,index){
                if(row.appName){
                    return row.appName+" ("+row.nodeName+")";
                }
            }},
            {field: 'logFile', title: '日志文件', width: 300,halign:'center',align:'center'},
            {field: 'level', title: '日志等级', width: 80,halign:'center',align:'center',
                styler: function(value,row,index){
                    if (value==="error"){
                        return 'color:red;font-weight:bold;';
                    }else if(value==="warn"){
                        return 'color:#fdcf00;font-weight:bold;';
                    }
                }
            },
            {field: 'date', title: '日期', width: 150,halign:'center',align:'center',
            formatter:function (value,row,index) {
                if(row.date){
                    return row.date+"  "+row.time;
                }else{
                    return value;
                }
            }},
            {field: 'content', title: '日志内容', width: 600,halign:'center',align:'center'},

        ]],
        onLoadSuccess:function (data) {
            total = dg.datagrid('getData').total;
            var pager = dg.datagrid('getPager');
            pager.pagination({
                onSelectPage: function (pageNumber, size) {
                    var appCode = $("#appCode").combobox("getValue");
                    var nodeCode = $("#nodeCode").combobox("getValue");
                    var level = $('#level').combobox("getValue");
                    var startTime = $('#startTime').datetimebox("getValue");
                    var endTime = $('#endTime').datetimebox("getValue");
                    var content=$("#conSearch").val();
                    pageSize = size;
                    pageNum = pageNumber == 0 ?1:pageNumber;
                    var url = contextPath+'/console/log/listByPageForEasyui?pageSize='+size+'&pageNo='+pageNum;
                    $.get(
                        url,
                        {
                            appCode:appCode,
                            nodeCode:nodeCode,
                            endTime:endTime,
                            startTime: startTime,
                            level:level,
                            content:content,
                        },
                        function (data) {
                            dg.datagrid('loadData', data);
                        },
                        'json'
                    );

                }
            });
        },
        onClickRow : function(index, rowData) {
            upCount=0;
            downCount=0;
            Ajax({
                url: contextPath+"/console/log/detailView/"+rowData.id,
                isMask : true,
                success: function (data) {
                    $('#content_id').val(rowData.id);
                    if (data.success) {
                        var app = data.data;
                        for (var key in app) {
                            try {
                                $("#detail-dlg").find("#" + key).html(JsonUti.convertToStringHtml(jQuery.parseJSON(app[key])));
                            } catch (e) {
                                $("#detail-dlg").find("#" + key).html(app[key]);
                            }
                        }
                        $("#detail-dlg").dialog("open");
                    }else {
                        apexLayer.Error(data.note);
                    }

                }
            });
        }
    });

    $("#detail-dlg").dialog({
        title:'详细信息',
        width: 800,
        height: 550,
        closed: true,
        cache: false,
        modal: true,
        shadow: false,
        resizable:true,
        maximizable:true,
        onClose: function () {
            $('#content').siblings().remove()
        },
    });
});

$(function(){
    $("#look").click(function(data) {//查询
        searchByCondition();
    });
    $("#reset").click(function(){//重置
        doReset();
    });
    $("#search_up").click(function () {//向上搜索
        searchUp();
    });
    $("#search_down").click(function () {//向下搜索
        searchDown();
    });


    $("#appCode").combobox({//应用
        method: 'get',
        url: contextPath + '/console/app/getAllApp',
        valueField: 'appCode',
        textField: 'appName',
        editable: false,
        panelHeight:　'auto',
        //:　'----请选择----',
        onSelect:function (rec) {
            $("#nodeCode").combobox('clear');
            $("#nodeCode").combobox('reload', contextPath + '/console/appNode/getAllNode?appCode='+rec.appCode);
        },
        onLoadSuccess: function (data) {

        },
        onLoadError: function () {

        },
        onShowPanel: function () {
            $(this).combobox('panel').height();

        },
        onChange: function (newValue,oldValue) {
        }
    });

    $("#nodeCode").combobox({//应用
        method: 'get',
        url: contextPath + '/console/appNode/getAllNode',
        valueField: 'nodeCode',
        textField: 'nodeName',
        editable: false,
        panelHeight:　'auto',
        //:　'----请选择----',
        onLoadSuccess: function (data) {

        },
        onLoadError: function () {

        },
        onShowPanel: function () {
            $(this).combobox('panel').height();

        },
        onChange: function (newValue,oldValue) {
        }
    });

    //设置当前日期为默认时间
    var curr_time = new Date();
    $("#startTime").datetimebox("setValue",myformatter(curr_time));
    $("#endTime").datetimebox("setValue",myformatter(curr_time));
});

function doReset() {
    // {} 作用是清除之前的查询条件
    dg.datagrid('load', {});
    $('#appCode').combobox('reset');
    $('#nodeCode').combobox('reset');
    $('#level').combobox('reset');
    var curr_time = new Date();
    $("#startTime").datetimebox("setValue",myformatter(curr_time));
    $("#endTime").datetimebox("setValue",myformatter(curr_time));
}

function searchUp(){//上翻
    upCount= upCount+1;
    var id=$("#content_id").val();
    var page=upCount;
    var type=0;
    Ajax({
        url: contextPath+"/console/log/queryLogs",
        isMask : true,
        data:{
          id:id,
            page:page,
            type:type
        },
        success: function (data) {
            if (data.code>0) {
                console.log(data.rows);
                var str="";
                for(var i=99;i>=0;i--){
                    str=str+"<li style='margin-top: 8px;'>"+"<span>"+data.rows[i].id+"</span>"+data.rows[i].content+"</li>";
                }
                $('#contentUl').find("li:first-child").before(str);
            }else {
                apexLayer.Error(data.note);
            }

        }
    });

}

function searchDown(){//下翻
    downCount= downCount+1;
    var id=$("#content_id").val();
    var page=downCount;
    var type=1;
    Ajax({
        url: contextPath+"/console/log/queryLogs",
        isMask : true,
        data:{
            id:id,
            page:page,
            type:type
        },
        success: function (data) {
            if (data.code>0) {
                console.log(data.rows);
                var str="";
                for(var i=0;i<100;i++){
                    str=str+"<li style='margin-top: 8px;'>"+"<span>"+data.rows[i].id+"</span>"+data.rows[i].content+"</li>";
                }
                $('#contentUl').find("li:last-child").after(str);
            }else {
                apexLayer.Error(data.note);
            }

        },
        error:function () {
            if(upCount>0){
                upCount= upCount-1;
            }
        }
    });
}

function searchByCondition(num,size) {
    var appCode = $("#appCode").combobox("getValue");
    var nodeCode = $("#nodeCode").combobox("getValue");
    var startTime= $('#startTime').datetimebox("getValue");
    var endTime = $('#endTime').datetimebox("getValue");
    var level = $('#level').combobox("getValue");
    var pager = dg.datagrid('getPager');
    var content=$("#conSearch").val();
    var currPageSize = pager.data("pagination" ).options.pageSize;
    size=currPageSize;
    var pageNum = num==null||num==undefined?1:num;
    var pageSize = size==null||size==undefined?20:size;
    var url =contextPath+'/console/log/listByPageForEasyui?pageSize='+pageSize+'&pageNo=1';
    pager.pagination('refresh',{	// 改变选项，并刷新分页栏信息
        pageSize:pageSize,
        pageNumber: 1,
    });
    $.get(
        url,
        {
            appCode:appCode,
            nodeCode:nodeCode,
            endTime:endTime,
            startTime: startTime,
            level:level,
            content:content,
        },
        function (data) {
            dg.datagrid('loadData', data);
        },
        'json'
    );
}

//日期格式化
function myformatter(date){
    var y = date.getFullYear();
    var m = date.getMonth()+1;
    var d = date.getDate();
    var HH=date.getHours();
    var MM=date.getMinutes();
    var SS=date.getSeconds();
    return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d)+" "+HH+":"+MM+":"+SS;
}
